package com.zhiyin.seal.service.impl;

import com.google.common.collect.Lists;
import com.zhiyin.seal.dao.CompanyUserDao;
import com.zhiyin.seal.dao.UserDao;
import com.zhiyin.seal.entity.AdminFunctionEntity;
import com.zhiyin.seal.entity.AdminSealEntity;
import com.zhiyin.seal.entity.CompanyUserEntity;
import com.zhiyin.seal.entity.UserEntity;
import com.zhiyin.seal.entity.bo.AdminFunctionBo;
import com.zhiyin.seal.entity.bo.AdminSealBo;
import com.zhiyin.seal.entity.bo.UserBo;
import com.zhiyin.seal.service.UserService;
import com.zhiyin.seal.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private CompanyUserDao companyUserDao;

    @Override
    public UserBo register(UserBo userBo) {
        UserEntity userEntity = BeanUtil.copy(userBo,UserEntity.class);
        int result = userDao.insertUser(userEntity);
        if(result > 0){
            return getUserInfo(userBo.getOpenId());
        }

        return null;
    }

    @Override
    public UserBo getUserInfo(String openId) {
        return BeanUtil.copy(userDao.getUserInfoByOpenId(openId),UserBo.class);
    }

    @Override
    public List<UserBo> getAllUserList(String keywords) {
        return BeanUtil.copyList(userDao.selectAllUserList(keywords),UserBo.class);
    }

    @Override
    public UserBo getUserInfoById(Long id) {
        return BeanUtil.copy(userDao.getUserInfoById(id),UserBo.class);
    }

    @Override
    public UserBo getUserInfoByIdAndCompany(Long userId,Long companyId){
        CompanyUserEntity companyUserEntity = companyUserDao.selectCompanyUser(userId,companyId);
        if(companyUserEntity != null){
            UserEntity userEntity = userDao.getUserInfoById(companyUserEntity.getUserId());
            if(userEntity != null){
                UserBo userBo = BeanUtil.copy(userEntity,UserBo.class);
                userBo.setNickName(companyUserEntity.getUserAlias());

                return userBo;
            }
        }

        return null;
    }

    @Override
    public List<UserBo> getUserListByUserIdsAndCompany(List<Long> userIdList,Long companyId){
        if(userIdList.size() == 0){
            userIdList.add(0l);
        }
        List<CompanyUserEntity> companyUserEntityList = companyUserDao.selectUserListByIdsAndCompanyId(userIdList,companyId);
        if(companyUserEntityList != null){
            List<UserBo> userBoList = Lists.newArrayList();
            for (CompanyUserEntity companyUserEntity : companyUserEntityList){
                UserEntity userEntity = userDao.getUserInfoById(companyUserEntity.getUserId());
                if(userEntity != null){
                    UserBo userBo = BeanUtil.copy(userEntity,UserBo.class);
                    userBo.setNickName(companyUserEntity.getUserAlias());
                    userBoList.add(userBo);
                }
            }
            return userBoList;
        }

        return null;
    }

    @Override
    public List<UserBo> getUserListByIds(List<Long> userIds) {
        return BeanUtil.copyList(userDao.getUserListByIds(userIds),UserBo.class);
    }

    @Override
    public Boolean updateUserInfo(UserBo userBo) {
        UserEntity userEntity = BeanUtil.copy(userBo,UserEntity.class);

        return userDao.updateUser(userEntity) > 0;
    }

    @Override
    public List<AdminSealBo> getAdminSealList(Long userId, Long companyId) {
        return BeanUtil.copyList(userDao.selectAdminSealList(userId,companyId),AdminSealBo.class);
    }

    @Override
    public Boolean updateAdminSeal(AdminSealBo adminSealBo) {
        AdminSealEntity adminSealEntity = BeanUtil.copy(adminSealBo,AdminSealEntity.class);
        return userDao.insertAdminSeal(adminSealEntity) > 0;
    }

    @Override
    public List<AdminFunctionBo> getAdminFunctionList(Long userId, Long companyId) {
        return BeanUtil.copyList(userDao.selectAdminFunctionList(userId,companyId),AdminFunctionBo.class);
    }

    @Override
    public Boolean updateAdminFunction(AdminFunctionBo adminFunctionBo) {
        AdminFunctionEntity adminFunctionEntity = BeanUtil.copy(adminFunctionBo,AdminFunctionEntity.class);
        return userDao.insertAdminFunction(adminFunctionEntity) > 0;
    }

    @Override
    public Integer getUserCount() {
        return userDao.selectUserCount();
    }
}